using System;
using System.IO;
using Microsoft.Win32;
using Core;

namespace Services
{
	internal class ApplicationSettingsService : IApplicationSettingsService
	{
		private const string InstallationPathRegistryKeyName = "InstallationPath";

		private const string CompilerPathRegistryKeyName = "CompilerPath";

		private const string CompilerInstallersPathRegistryKeyName = "InstallersPath";

		private const string DatabaseUtilitiesPathRegistryKeyName = "DatabaseUtilitiesPath";

		private const string DefaultStorePathRegistryKeyName = "DefaultStorePath";

		private const string HelpFilesPathRegistryKeyName = "HelpFilesPath";

		private const string BrowsePathRegistryKeyName = "BrowsePath";

		private const string IniFileFolderPath = "DeltaTau\\INI";

		private const string EtherCATConfigurationRegistryKeyName = "EtherCATConfigurationPath";

		private const string PlotControlFolderPath = "PlotControl";

		private readonly IIniFileService iniFileService;

		private readonly IRegistryService registryService;

		private readonly IEnvironmentFolderService environmentFolderService;

		private string installationPath;

		private string compilerPath;

		private string compilerInstallersPath;

		private string databaseUtilitiesPath;

		private string defaultStorePath;

		private string helpFilesPath;

		private string iniFilesPath;

		private string plotControlPath;

		private string ethercatConfigurationPath;

		private DatabaseSettings databaseSettings;

		private string browsePath;

		public string InstallationPath => installationPath ?? (installationPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "InstallationPath"));

		public string CompilerPath => compilerPath ?? (compilerPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\Compilers", "CompilerPath"));

		public string CompilerInstallersPath => compilerInstallersPath ?? (compilerInstallersPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\Compilers", "InstallersPath"));

		public string DatabaseUtilitiesPath => databaseUtilitiesPath ?? (databaseUtilitiesPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "DatabaseUtilitiesPath"));

		public string DefaultStorePath => defaultStorePath ?? (defaultStorePath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "DefaultStorePath"));

		public string HelpFilesPath => helpFilesPath ?? (helpFilesPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "HelpFilesPath"));

		public string IniFilesPath => iniFilesPath ?? (iniFilesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DeltaTau\\INI"));

		public string PlotControlPath => plotControlPath ?? (plotControlPath = Path.Combine(InstallationPath, "PlotControl"));

		public string EtherCATConfigurationPath => ethercatConfigurationPath ?? (ethercatConfigurationPath = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "EtherCATConfigurationPath"));

		public string BrowsePath
		{
			get
			{
				return browsePath ?? (browsePath = (registryService.DoesRegistryKeyNameExist(RegistryLocation.LocalMachine, "SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "BrowsePath") ? GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "BrowsePath") : environmentFolderService.MyDocumentsFolder));
			}
			set
			{
				if (!string.IsNullOrEmpty(value) && !string.Equals(value, browsePath, StringComparison.InvariantCultureIgnoreCase))
				{
					browsePath = value;
					SetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE", "BrowsePath", browsePath);
				}
			}
		}

		public DatabaseSettings DatabaseSettings
		{
			get
			{
				string path = string.Format("ppmac{0}.ini", "4");
				if (databaseSettings == null)
				{
					string registryValue = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE\\DataBase", "InstallationPath");
					string registryValue2 = GetRegistryValue("SOFTWARE\\Delta Tau Data Systems Inc\\4\\PowerPMAC IDE\\DataBase", "InstanceName");
					string filePath = Path.Combine(registryValue, path);
					string dataFilesPath = iniFileService.ReadStringValue(filePath, registryValue2, "datadir");
					int port = iniFileService.ReadIntValue(filePath, registryValue2, "port");
					databaseSettings = new DatabaseSettings(registryValue, dataFilesPath, registryValue2, port);
				}
				return databaseSettings;
			}
		}

		public ApplicationSettingsService(IIniFileService iniFileService, IRegistryService registryService, IEnvironmentFolderService environmentFolderService)
		{
			this.iniFileService = iniFileService;
			this.registryService = registryService;
			this.environmentFolderService = environmentFolderService;
		}

        public string GetRegistryValue(string path, string name)
		{
			string text = null;
			try
			{
				text = registryService.GetValue<string>(RegistryLocation.LocalMachine, path, name);
			}
			catch (Exception)
			{
			}
			if (text == null)
			{
				throw new InvalidOperationException($"ApplicationSettingsService - Missing Registry value '{name}' for registry key '{Registry.LocalMachine.Name}\\{path}'");
			}
			return text;
		}

        public void SetRegistryValue(string path, string name, string value)
		{
			try
			{
				registryService.SetValue(RegistryLocation.LocalMachine, path, name, value, RegistryValueType.String);
			}
			catch (Exception)
			{
			}
		}
	}
}
